<template>
  <view v-if="show" class="auth-modal">
    <view class="modal-mask" @click="preventClose"></view>
    <view class="modal-content">
      <view class="modal-header">
        <text class="modal-title">密码验证</text>
      </view>
      <view class="modal-body">
        <text class="modal-desc">请输入访问密码</text>
        <input 
          type="password" 
          v-model="password" 
          class="password-input" 
          placeholder="请输入密码"
          @confirm="handleVerify"
        />
        <view v-if="errorMessage" class="error-message">
          <text class="error-text">{{ errorMessage }}</text>
        </view>
      </view>
      <view class="modal-footer">
        <button class="verify-btn" @click="handleVerify">验证</button>
      </view>
    </view>
  </view>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { verifyPassword, markUserAuthenticated } from '@/utils/auth';

export default defineComponent({
  name: 'AuthModal',
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  emits: ['success'],
  setup(props, { emit }) {
    const password = ref('');
    const errorMessage = ref('');

    // 验证密码
    const handleVerify = () => {
      if (!password.value) {
        errorMessage.value = '请输入密码';
        return;
      }

      console.log('开始验证密码:', password.value);
      
      if (verifyPassword(password.value)) {
        // 验证成功
        console.log('密码验证成功');
        markUserAuthenticated();
        errorMessage.value = '';
        password.value = '';
        emit('success');
        
        uni.showToast({
          title: '验证成功',
          icon: 'success'
        });
      } else {
        // 验证失败
        console.log('密码验证失败');
        errorMessage.value = '密码错误，请重新输入';
        password.value = '';
      }
    };

    // 防止点击遮罩关闭
    const preventClose = () => {
      // 不做任何操作，防止用户关闭弹窗
    };

    return {
      password,
      errorMessage,
      handleVerify,
      preventClose
    };
  }
});
</script>

<style lang="scss" scoped>
.auth-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.modal-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
}

.modal-content {
  position: relative;
  width: 600rpx;
  max-width: 90vw;
  background-color: #ffffff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
}

.modal-header {
  padding: 40rpx 30rpx 20rpx;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
}

.modal-body {
  padding: 40rpx 30rpx;
}

.modal-desc {
  display: block;
  font-size: 28rpx;
  color: #666666;
  text-align: center;
  margin-bottom: 30rpx;
}

.password-input {
  width: 100%;
  height: 80rpx;
  border: 2rpx solid #e0e0e0;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 32rpx;
  color: #333333;
  box-sizing: border-box;
  outline: none;
  
  &:focus {
    border-color: #007aff;
    outline: none;
  }
}

.error-message {
  margin-top: 20rpx;
  text-align: center;
}

.error-text {
  font-size: 24rpx;
  color: #ff3b30;
}

.modal-footer {
  padding: 20rpx 30rpx 40rpx;
}

.verify-btn {
  width: 100%;
  height: 80rpx;
  background-color: #007aff;
  color: #ffffff;
  font-size: 32rpx;
  font-weight: bold;
  border-radius: 8rpx;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  
  &:active {
    background-color: #0056cc;
  }
}
</style>
